package com.sumer.brain.sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sumer.brain.common.model.R;
import com.sumer.brain.sys.model.entity.SysRole;
import com.sumer.brain.sys.service.SysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@Tag(name = "角色管理")
@RequestMapping("/sysRole")
public class SysRoleController {

    private final SysRoleService sysRoleService;

    @Operation(summary = "分页查询角色列表", description = "分页查询角色列表", tags = "查询")
    @GetMapping("page")
    public R<Page<SysRole>> page() {
        return R.ok(sysRoleService.page(new Page<>()));
    }

    @Operation(summary = "根据id查询角色信息", description = "根据id查询角色信息", tags = "查询")
    @GetMapping("{id}")
    public R<SysRole> getById(@PathVariable("id") Long id) {
        return R.ok(sysRoleService.getById(id));
    }

    @Operation(summary = "新增角色", description = "新增角色", tags = "新增")
    @PostMapping()
    public R<Boolean> add(@RequestBody @Validated SysRole sysRole) {
        // 插入角色数据
        boolean save = sysRoleService.save(sysRole);
        // 保存角色菜单关联数据
        if (sysRole.getMenuIds() != null && sysRole.getMenuIds().size() > 0) {
            sysRoleService.saveRoleMenu(sysRole);
        }
        return R.ok(save);
    }

    @Operation(summary = "编辑角色", description = "编辑角色", tags = "编辑")
    @PutMapping
    public R<Boolean> edit(@RequestBody SysRole sysRole) {
        boolean b = sysRoleService.updateById(sysRole);
        sysRoleService.updateRoleMenu(sysRole);
        return R.ok(b);
    }

    @Operation(summary = "根据id批量删除角色", description = "根据id批量删除角色", tags = "删除")
    @DeleteMapping("{ids}")
    public R<Boolean> deleteById(@PathVariable("ids") String ids) {
        return R.ok(sysRoleService.delByIds(ids));
    }
}
